﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Web.Mvc;
using RedSand.Site.Models;

namespace RedSand.Site.Controllers
{
    public class ComponentsController : BaseController
    {
        
		#region admin manager
		public ActionResult Manager()
        {
            return View(new ManagerModel { Title = "Components Manager" });
        }

        [AjaxOrChildAction]
        public ActionResult List(int parent = 0)
        {
            ViewBag.Parent = parent;
            return PartialView(parent == 0 ? "ListParent" : "ListChild", RsDataManager.GetList<ComponentsInfo>("ComponentsGetList", new { ParentID = parent }));
        }

        [AjaxOrChildAction, HttpGet]
        public ActionResult Create(int count, int parent = 0)
        {
            return PartialView(parent == 0 ? "CreateParent" : "CreateChild", new ComponentsInfo { Order = count + 1, ParentID = parent });
        }
        [AjaxOrChildAction, HttpPost]
        public ActionResult Create(ComponentsInfo info, int count)
        {
            if (ModelState.IsValid)
            {
                if (RsDataManager.Save("ComponentsSave", info) > 0)
                {
                    AddMessage(Message.CreateSuccess);
                    return RedirectToAction("List", new { parent = info.ParentID });
                }
                else
                {
                    return AjaxMessage(Message.CreateError);
                }
            }
            return AjaxModelError();
        }

        [AjaxOrChildAction, HttpGet]
        public ActionResult Edit(int id, int count)
        {
            var info = RsDataManager.GetInfo<ComponentsInfo>("ComponentsGetInfo", "ComponentsID", id);
            return PartialView(info.ParentID == 0 ? "EditParent" : "EditChild", info);
        }
        [AjaxOrChildAction, HttpPost]
        public ActionResult Edit(ComponentsInfo info)
        {
            if (ModelState.IsValid)
            {
                if (RsDataManager.Save("ComponentsSave", info) > 0)
                {
                    AddMessage(Message.UpdateSuccess);
                    return RedirectToAction("List", new { parent = info.ParentID });
                }
                else
                {
                    return AjaxMessage(Message.UpdateError);
                }
            }
            return AjaxModelError();
        }

        [AjaxOrChildAction]
        public ActionResult Delete(int id, int parent = 0)
        {
            if (RsDataManager.Delete("ComponentsDelete", "ComponentsID", id))
            {
                AddMessage(Message.DeleteSuccess);
                return RedirectToAction("List", new { parent = parent });
            }
            return AjaxMessage(Message.DeleteError);
        }

		#endregion
    }
}
